import { Page } from "playwright";
import { consoleLog } from "./utils";

// 页面方法重写，需要检测元素存在
export default class PageService {
  public page: Page;
  constructor(page: Page) {
    this.page = page
  }
  async fill(selector, value){
    const isVisible = await this.page.isVisible(selector);
    if(isVisible) {
      await this.page.fill(selector, value);
    } else {
      await consoleLog(`页面元素: ${selector} 不存在`);
    }
  }
  async click (selector) {
    const isVisible = await this.page.isVisible(selector);
    if(isVisible) {
      await this.page.click(selector);
    } else {
      await consoleLog(`页面元素: ${selector} 不存在`);
    }
  }
  async innerHTML (selector){
    const isVisible = await this.page.isVisible(selector);
    if(isVisible) {
      return await this.page.innerHTML(selector);
    } else {
      await consoleLog(`页面元素: ${selector} 不存在`);
    }
  }
  async innerText (selector){
    const isVisible = await this.page.isVisible(selector);
    if(isVisible) {
      return await this.page.innerHTML(selector);
    } else {
      await consoleLog(`页面元素: ${selector} 不存在`);
    }
  }
}